<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function __construct(){
		parent::__construct();
		$this->load->model('user/user_model','user_model');
		$this->load->model('admin/banner_model','banner_model');
		$this->lang->load('main', 'thai');
	}
	
	public function index()
	{
		$this->load->model('admin/company_model','company_model');
		$this->load->model('admin/country_model','country_model');
		$this->load->model('admin/province_model','province_model');
		$page_limit = $this->config->item('company_per_page');
		$main_cat_gold = $this->company_model->get_company_lists(0,1,$page_limit,2,0,0);
		
		$data['company_list'] = array();
		$i = 0;
		foreach($main_cat_gold as $v){
			if($i%2){
				$data['company_list'][$i]['class'] = 'odd';
			}else{
				$data['company_list'][$i]['class'] = 'even';
			}
			$data['company_list'][$i]['id'] = $v->id;
			$data['company_list'][$i]['name'] = $v->name;
			$data['company_list'][$i]['address'] = $v->address;
			$data['company_list'][$i]['district'] = $v->district;
			$data['company_list'][$i]['province'] = $this->province_model->getProvinceNameByCatId($v->province);
			$data['company_list'][$i]['postcode'] = $v->postcode;
			$data['company_list'][$i]['country'] = $this->country_model->getCountryNameByCatId($v->country);
			$data['company_list'][$i]['logo'] = $v->logo;
			$data['company_list'][$i]['img'] = $v->img;
			$data['company_list'][$i]['website_name'] = str_replace('http://','',$v->website);
			$data['company_list'][$i]['website'] = 'http://'.str_replace('http://','',$v->website);
			$data['company_list'][$i]['email'] = $v->email;
			$data['company_list'][$i]['tel'] = $v->tel;
			$data['company_list'][$i]['fax'] = $v->fax;
			$data['company_list'][$i]['brand'] = $v->brand;
			$data['company_list'][$i]['email'] = $v->email;
			$data['company_list'][$i]['gold_icon'] = '<img src="'.base_url().'assets/image/gold.gif" alt="สมาชิก Gold Supplier" title="สมาชิก Gold Supplier" class="gold_icon" />';
			$data['company_list'][$i]['agent_icon'] = '';
			if($v->agent==1){
				$data['company_list'][$i]['agent_icon'] = '<img src="'.base_url().'assets/image/agent.gif" alt="เปิดรับตัวแทนจำหน่าย" title="เปิดรับตัวแทนจำหน่าย" class="agent_icon"  />';
			}
			
			$i++;
		}
		
		$g = $page_limit-$i;
		if($g>0){
			$main_cat_general = $this->company_model->get_company_lists(0,1,$g,1,0,0);
			$i = $g;
			foreach($main_cat_general as $v){
				if($i%2){
					$data['company_list'][$i]['class'] = 'odd';
				}else{
					$data['company_list'][$i]['class'] = 'even';
				}
				$data['company_list'][$i]['id'] = $v->id;
				$data['company_list'][$i]['name'] = $v->name;
				$data['company_list'][$i]['address'] = $v->address;
				$data['company_list'][$i]['district'] = $v->district;
				$data['company_list'][$i]['province'] = $this->province_model->getProvinceNameByCatId($v->province);
				$data['company_list'][$i]['postcode'] = $v->postcode;
				$data['company_list'][$i]['country'] = $this->country_model->getCountryNameByCatId($v->country);
				$data['company_list'][$i]['logo'] = $v->logo;
				$data['company_list'][$i]['img'] = $v->img;
				$data['company_list'][$i]['website_name'] = str_replace('http://','',$v->website);
				$data['company_list'][$i]['website'] = 'http://'.str_replace('http://','',$v->website);
				$data['company_list'][$i]['email'] = $v->email;
				$data['company_list'][$i]['tel'] = $v->tel;
				$data['company_list'][$i]['fax'] = $v->fax;
				$data['company_list'][$i]['brand'] = $v->brand;
				$data['company_list'][$i]['email'] = $v->email;
				$data['company_list'][$i]['gold_icon'] = '';
				$data['company_list'][$i]['agent_icon'] = '';
				if($v->agent==1){
					$data['company_list'][$i]['agent_icon'] = '<img src="'.base_url().'assets/image/agent.gif" alt="เปิดรับตัวแทนจำหน่าย"  title="เปิดรับตัวแทนจำหน่าย" class="agent_icon" />';
				}
				$i++;
			}
		}
		
		$header['title'] = config_item('site_name');
		$banner['user_block'] = get_user_box();
		
		$this->template->set_template('home');
		$this->template->write_view('header','master/header',$header);
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('content','index',$data);
		$this->template->write_view('footer','master/footer');
		$this->template->render();
	}
	
	public function about()
	{
		$header['title'] = config_item('site_name').' : เกี่ยวกับเรา';
		
		$this->template->set_template('page');
		$this->template->write_view('header','master/header',$header , false);
		
		//$banner = $this->banner_model->get_banner();
		$banner['user_block'] = get_user_box();
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		
		$this->template->write_view('search_form','master/page_search');
		$this->template->write_view('content', 'about');
		$this->template->write_view('footer','master/footer');
		$this->template->render();
	}
	
	public function contact()
	{
		$header['title'] = config_item('site_name').' : '.$this->lang->line('contact');
		
		$data['email'] = 'E-mail';
		$data['name'] = 'Your name';
		$data['tel'] = 'Phone number';
		
		if(($this->session->userdata('email'))!=''){
			$data['email'] = $this->session->userdata('email');
			$user_info = $this->user_model->getUserInfo($data['email']);
			$data['name'] = $user_info['firstname'].' '.$user_info['lastname'];
			$data['tel'] = $user_info['tel'];
		}
		
		$this->template->set_template('page');
		$this->template->write_view('header','master/header',$header , false);
		
		//$banner = $this->banner_model->get_banner();
		$banner['user_block'] = get_user_box();
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		
		$this->template->write_view('search_form','master/page_search');
		$this->template->write_view('content', 'contact',$data);
		$this->template->write_view('footer','master/footer');
		$this->template->render();
	}
	
	function contact_send()
	{
		$this->load->library('cimailer','cimailer');
		$file = APPPATH.'../assets/mail/contact.txt';
		$handle = fopen($file,'r');
		$body = '';
		if ($handle) {
			while (!feof($handle)) {
				$file_ = fgets($handle, 4096);
				$body .= $file_."<br>";
			}
			fclose($handle);
		}
		
		try{
			//$this->cimailer->IsMail();
			$this->cimailer->CharSet="utf-8";
			$this->cimailer->IsHTML(true);
			$this->cimailer->From =  $_POST['email'];
			
			$body = str_replace('{NAME}',$_POST['name'],$body);
			$body = str_replace('{EMAIL}',$_POST['email'],$body);
			$body = str_replace('{TEL}',$_POST['phone'],$body);
			$body = str_replace('{MESSAGE}',$_POST['msg'],$body);
			
			$this->cimailer->FromName = $_POST['name'];
			$this->cimailer->AddReplyTo($_POST['email'] , $_POST['name']);
			$this->cimailer->AddAddress($this->config->item('main_email'));
			$this->cimailer->Subject = $this->config->item('site_name').' : '.$this->lang->line('contact_mail_title');
			$this->cimailer->Body = $body;
			$this->cimailer->Send();
			$data['message'] = $this->lang->line('contact_form_complete');
		} catch (phpmailerException $e) {
			echo $e->errorMessage(); //Pretty error messages from PHPMailer
			$data['message'] = $this->lang->line('mail_not_send');
		} catch (Exception $e) {
			echo $e->getMessage(); //Boring error messages from anything else!
			$data['message'] = $this->lang->line('mail_not_send');
		}
		
		$banner['user_block'] = get_user_box();
		$data['block_title'] = $this->lang->line('paid_form');
		$header['title'] = $this->config->item('site_name').' : '.$this->lang->line('contact');
		
		$this->template->set_template('page');
		$this->template->write_view('header','home/master/header',$header , false);
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		$this->template->write_view('search_form','home/master/page_search');
		$this->template->write_view('content', 'contact_result_message',$data);
		$this->template->write_view('footer','home/master/footer');
		$this->template->render();
	}
	
	public function directory()
	{
		$this->load->model('admin/category_model','category_model');
		$main_cat = $this->category_model->lists(0,1,500);
		$data['catlist'] = array();
		$i = 0;
		foreach($main_cat as $v){
			$data['catlist'][$i]['id'] = $v->id;
			$data['catlist'][$i]['name_th'] = $v->name_th;
			$data['catlist'][$i]['img'] = $v->img;
			
			$data['catlist'][$i]['sub'] = $this->category_model->lists($v->id,1,500);
			
			$lenght = 0;
			$max = 360;
			$pop = 0;
			foreach($data['catlist'][$i]['sub'] as $val){
				
				if(($lenght+strlen($val->name_th) < $max)){
					$lenght += strlen($val->name_th);
				}else{
					array_pop($data['catlist'][$i]['sub']);
				}
			}
			//if($v->id == 28){
			//	echo $lenght;
			//}
			$i++;
		}
		
		$header['title'] = config_item('site_name').' : '.'สารบัญสินค้าและบริการ';
		
		$this->template->set_template('page');
		$this->template->write_view('header','master/header',$header , false);
		
		//$banner = $this->banner_model->get_banner();
		$banner['user_block'] = get_user_box();
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		
		$this->template->write_view('search_form','master/page_search');
		$this->template->write_view('content', 'directory',$data);
		$this->template->write_view('footer','master/footer');
		$this->template->render();
	}
	
	public function category($id_name)
	{
		$id = explode('-',$id_name);
		$this->load->model('admin/category_model','category_model');
		$main_cat = $this->category_model->lists($id[0],1,500);
		$data['catlist'] = array();
		$data['catid'] = $id[0];
		$data['cat_name'] = $this->category_model->getCatNameByCatId($id[0]);
		$i = 0;
		foreach($main_cat as $v){
			$data['catlist'][$i]['id'] = $v->id;
			$data['catlist'][$i]['name_th'] = $v->name_th;
			$data['catlist'][$i]['name_en'] = $v->name_en;
			$data['catlist'][$i]['img'] = $v->img;
			$data['catlist'][$i]['sub'] = $this->category_model->lists($v->id,1,500);
			$i++;
		}
		
		$data['block_title'] = $data['cat_name'];
		
		$header['title'] = config_item('site_name').' : '.$data['cat_name'];
		
		$this->template->set_template('page');
		$this->template->write_view('header','master/header',$header , false);
		
		//$banner = $this->banner_model->get_banner();
		$banner['user_block'] = get_user_box();
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		
		$this->template->write_view('search_form','master/page_search');
		$this->template->write_view('content', 'category',$data);
		$this->template->write_view('footer','master/footer');
		$this->template->render();
	}
	
	public function category_company($id_name , $subid_name , $page=1)
	{
		$this->load->model('admin/category_model','category_model');
		
		$id = explode('-',$id_name);
		$subid = explode('-',$subid_name);
		$data['cat_name'] = $this->category_model->getCatNameByCatId($id[0]);
		$data['sub_cat_name'] = $this->category_model->getCatNameByCatId($subid[0]);
		
		$this->load->model('admin/company_model','company_model');
		$this->load->model('admin/country_model','country_model');
		$this->load->model('admin/province_model','province_model');
		
		$page_limit = $this->config->item('company_per_page');
		$count_company = count($this->company_model->get_company_lists($subid[0],1,0,0,1,0));
		$count_gold_company = count($this->company_model->get_company_lists($subid[0],1,0,2,1,0));
		$gold_page = ceil($count_gold_company/$page_limit);
		$data['company_nav'] = get_company_nav($count_company , $page_limit , $page , site_url().$this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3).'/%page%');
		
		$main_cat_gold = array();
		$main_cat_general = array();
		$data['company_list'] = array();
		
		$main_cat_gold = $this->company_model->get_company_lists($subid[0],$page,$page_limit,2,1,0);
		$i = 0;
		foreach($main_cat_gold as $v){
			if($i%2){
				$data['company_list'][$i]['class'] = 'odd';
			}else{
				$data['company_list'][$i]['class'] = 'even';
			}
			$data['company_list'][$i]['id'] = $v->id;
			$data['company_list'][$i]['name'] = $v->name;
			$data['company_list'][$i]['address'] = $v->address;
			$data['company_list'][$i]['district'] = $v->district;
			$data['company_list'][$i]['province'] = $this->province_model->getProvinceNameByCatId($v->province);
			$data['company_list'][$i]['postcode'] = $v->postcode;
			$data['company_list'][$i]['country'] = $this->country_model->getCountryNameByCatId($v->country);
			$data['company_list'][$i]['logo'] = $v->logo;
			$data['company_list'][$i]['img'] = $v->img;
			$data['company_list'][$i]['website_name'] = str_replace('http://','',$v->website);
			$data['company_list'][$i]['website'] = 'http://'.str_replace('http://','',$v->website);
			$data['company_list'][$i]['email'] = $v->email;
			$data['company_list'][$i]['tel'] = $v->tel;
			$data['company_list'][$i]['fax'] = $v->fax;
			$data['company_list'][$i]['brand'] = $v->brand;
			$data['company_list'][$i]['email'] = $v->email;
			$data['company_list'][$i]['gold_icon'] = '<img src="'.base_url().'assets/image/gold.gif" alt="สมาชิก Gold Supplier" title="สมาชิก Gold Supplier" class="gold_icon" />';
			$data['company_list'][$i]['agent_icon'] = '';
			if($v->agent==1){
				$data['company_list'][$i]['agent_icon'] = '<img src="'.base_url().'assets/image/agent.gif" alt="เปิดรับตัวแทนจำหน่าย" title="เปิดรับตัวแทนจำหน่าย" class="agent_icon"  />';
			}
			$i++;
		}
		
		$g = $page_limit-$i;
		if($page>=$gold_page AND $g>0){
			if($gold_page==0){
				$start_plus = 0;
			}
			else if($page==$gold_page){
				$page = 1;
				$start_plus = 0;
				$page_limit = $g;
			}else{
				$start_plus = $page_limit-($count_gold_company%$page_limit);
				$g = 0;
				$page -= ($gold_page);
				$i = 0;
			}
			
			$main_cat_general = $this->company_model->get_company_lists($subid[0],$page,$page_limit,1,0,0,$start_plus);
			foreach($main_cat_general as $v){
				if($i%2){
					$data['company_list'][$i]['class'] = 'odd';
				}else{
					$data['company_list'][$i]['class'] = 'even';
				}
				$data['company_list'][$i]['id'] = $v->id;
				$data['company_list'][$i]['name'] = $v->name;
				$data['company_list'][$i]['address'] = $v->address;
				$data['company_list'][$i]['district'] = $v->district;
				$data['company_list'][$i]['province'] = $this->province_model->getProvinceNameByCatId($v->province);
				$data['company_list'][$i]['postcode'] = $v->postcode;
				$data['company_list'][$i]['country'] = $this->country_model->getCountryNameByCatId($v->country);
				$data['company_list'][$i]['logo'] = $v->logo;
				$data['company_list'][$i]['img'] = $v->img;
				$data['company_list'][$i]['website_name'] = str_replace('http://','',$v->website);
				$data['company_list'][$i]['website'] = 'http://'.str_replace('http://','',$v->website);
				$data['company_list'][$i]['email'] = $v->email;
				$data['company_list'][$i]['tel'] = $v->tel;
				$data['company_list'][$i]['fax'] = $v->fax;
				$data['company_list'][$i]['brand'] = $v->brand;
				$data['company_list'][$i]['email'] = $v->email;
				$data['company_list'][$i]['gold_icon'] = '';
				$data['company_list'][$i]['agent_icon'] = '';
				if($v->agent==1){
					$data['company_list'][$i]['agent_icon'] = '<img src="'.base_url().'assets/image/agent.gif" alt="เปิดรับตัวแทนจำหน่าย" title="เปิดรับตัวแทนจำหน่าย" class="agent_icon"  />';
				}
				$i++;
			}
		}
		
		$header['title'] = config_item('site_name').' : '.$data['cat_name'].' : '.$data['sub_cat_name'];
		$data['block_title'] = /*$data['cat_name'].' >> '.*/$data['sub_cat_name'];
		
		$this->template->set_template('page');
		$this->template->write_view('header','master/header',$header , true);
		
		//$banner = $this->banner_model->get_banner();
		$banner['user_block'] = get_user_box();
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		
		$this->template->write_view('search_form','master/page_search');
		$this->template->write_view('content', 'category_sub',$data);
		$this->template->write_view('footer','master/footer');
		$this->template->render();
	}
	
	public function agent($page=1)
	{
		$this->load->model('admin/category_model','category_model');
		
		$this->load->model('admin/company_model','company_model');
		$this->load->model('admin/country_model','country_model');
		$this->load->model('admin/province_model','province_model');
		
		$page_limit = $this->config->item('company_per_page');
		$count_company = count($this->company_model->get_company_lists(0,1,0,0,1,0));
		$count_gold_company = count($this->company_model->get_company_lists(0,1,0,2,1,0));
		$gold_page = ceil($count_gold_company/$page_limit);
		$data['company_nav'] = get_company_nav($count_company , $page_limit , $page , site_url().'agent/%page%');
		
		$main_cat_gold = array();
		$main_cat_general = array();
		$data['company_list'] = array();
		
		$main_cat_gold = $this->company_model->get_company_lists(0,$page,$page_limit,2,1,0);
		$i = 0;
		foreach($main_cat_gold as $v){
			if($i%2){
				$data['company_list'][$i]['class'] = 'odd';
			}else{
				$data['company_list'][$i]['class'] = 'even';
			}
			$data['company_list'][$i]['id'] = $v->id;
			$data['company_list'][$i]['name'] = $v->name;
			$data['company_list'][$i]['address'] = $v->address;
			$data['company_list'][$i]['district'] = $v->district;
			$data['company_list'][$i]['province'] = $this->province_model->getProvinceNameByCatId($v->province);
			$data['company_list'][$i]['postcode'] = $v->postcode;
			$data['company_list'][$i]['country'] = $this->country_model->getCountryNameByCatId($v->country);
			$data['company_list'][$i]['logo'] = $v->logo;
			$data['company_list'][$i]['img'] = $v->img;
			$data['company_list'][$i]['website_name'] = str_replace('http://','',$v->website);
			$data['company_list'][$i]['website'] = 'http://'.str_replace('http://','',$v->website);
			$data['company_list'][$i]['email'] = $v->email;
			$data['company_list'][$i]['tel'] = $v->tel;
			$data['company_list'][$i]['fax'] = $v->fax;
			$data['company_list'][$i]['brand'] = $v->brand;
			$data['company_list'][$i]['email'] = $v->email;
			
			$data['company_list'][$i]['gold_icon'] = '<img src="'.base_url().'assets/image/gold.gif" alt="สมาชิก Gold Supplier" title="สมาชิก Gold Supplier" class="gold_icon" />';
			$data['company_list'][$i]['agent_icon'] = '';
			if($v->agent==1){
				$data['company_list'][$i]['agent_icon'] = '<img src="'.base_url().'assets/image/agent.gif" alt="เปิดรับตัวแทนจำหน่าย" title="เปิดรับตัวแทนจำหน่าย" class="agent_icon"  />';
			}
			
			$i++;
		}
		
		$g = $page_limit-$i;
		if($page>=$gold_page AND $g>0){
			if($page==$gold_page){
				$page = 1;
				$start_plus = 0;
				$page_limit = $g;
			}else{
				if($count_gold_company>0){
					$start_plus = $page_limit-($count_gold_company%$page_limit);
					$g = 0;
					$page -= ($gold_page);
					$i = 0;
				}else{
					$start_plus = 0;
				}
			}
			
			$main_cat_general = $this->company_model->get_company_lists(0,$page,$page_limit,1,0,0,$start_plus);
			foreach($main_cat_general as $v){
				if($i%2){
					$data['company_list'][$i]['class'] = 'odd';
				}else{
					$data['company_list'][$i]['class'] = 'even';
				}
				$data['company_list'][$i]['id'] = $v->id;
				$data['company_list'][$i]['name'] = $v->name;
				$data['company_list'][$i]['address'] = $v->address;
				$data['company_list'][$i]['district'] = $v->district;
				$data['company_list'][$i]['province'] = $this->province_model->getProvinceNameByCatId($v->province);
				$data['company_list'][$i]['postcode'] = $v->postcode;
				$data['company_list'][$i]['country'] = $this->country_model->getCountryNameByCatId($v->country);
				$data['company_list'][$i]['logo'] = $v->logo;
				$data['company_list'][$i]['img'] = $v->img;
				$data['company_list'][$i]['website_name'] = str_replace('http://','',$v->website);
				$data['company_list'][$i]['website'] = 'http://'.str_replace('http://','',$v->website);
				$data['company_list'][$i]['email'] = $v->email;
				$data['company_list'][$i]['tel'] = $v->tel;
				$data['company_list'][$i]['fax'] = $v->fax;
				$data['company_list'][$i]['brand'] = $v->brand;
				$data['company_list'][$i]['email'] = $v->email;
				$data['company_list'][$i]['gold_icon'] = '';
				$data['company_list'][$i]['agent_icon'] = '';
				if($v->agent==1){
					$data['company_list'][$i]['agent_icon'] = '<img src="'.base_url().'assets/image/agent.gif" alt="เปิดรับตัวแทนจำหน่าย" title="เปิดรับตัวแทนจำหน่าย" class="agent_icon"  />';
				}
				$i++;
			}
		}
		
		$header['title'] = config_item('site_name').' : '.$this->lang->line('agent_list');
		
		$this->template->set_template('page');
		$this->template->write_view('header','master/header',$header , true);
		
		//$banner = $this->banner_model->get_banner();
		$banner['user_block'] = get_user_box();
		$data['block_title'] = $this->lang->line('agent_list');
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		
		$this->template->write_view('search_form','master/page_search');
		$this->template->write_view('content', 'agent',$data);
		$this->template->write_view('footer','master/footer');
		$this->template->render();
	}
	
	public function technology($p=1)
	{
		$this->load->model('admin/technology_model','technology_model');
		$data['techlist'] = $this->technology_model->lists($p,4);
		
		$header['title'] = config_item('site_name').' : '.'สารบัญสินค้าและบริการ';
		
		$this->template->set_template('page');
		$this->template->write_view('header','master/header',$header , false);
		
		//$banner = $this->banner_model->get_banner();
		$banner['user_block'] = get_user_box();
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		
		$this->template->write_view('search_form','master/page_search');
		$this->template->write_view('content', 'technology',$data);
		$this->template->write_view('footer','master/footer');
		$this->template->render();
	}
	
	public function getSubOption($id)
	{
		$this->load->model('admin/category_model','category_model');
		$result = ($this->category_model->lists($id,1,500));
		foreach($result as $v){
			echo "<option value='{$v->id}'>{$v->name_th}</option>";
		}
	}
	
	public function getProvinceOption($id=NULL)
	{
		$this->load->model('admin/province_model','province_model');
		
		if($id!==NULL){
			$result = ($this->province_model->lists($id,1,500));
		}else{
			$id = $_REQUEST['geography'];
			$result = ($this->province_model->lists($id,1,500));
		}
		
		foreach($result as $v){
			echo "<option value='{$v->PROVINCE_ID}'>{$v->PROVINCE_NAME}</option>";
		}
	}
	
	function banner()
	{
		$this->load->model('admin/banner_model','banner_model');
		$header['title'] = $this->config->item('site_name').' : '.$this->lang->line('banner');
		$data['block_title'] = $this->lang->line('banner');
		$data['banner_location'] = $this->banner_model->get_banner_location();
		
		$data['email'] = '';
		$data['firstname'] = '';
		$data['lastname'] = '';
		$data['tel'] = '';
		if(($this->session->userdata('email'))!=''){
			$data['email'] = $this->session->userdata('email');
			$user_info = $this->user_model->getUserInfo($data['email']);
			$data['firstname'] = $user_info['firstname'];
			$data['lastname'] = $user_info['lastname'];
			$data['tel'] = $user_info['tel'];
		}
		$data['detail'] = htmlspecialchars_decode($this->banner_model->get_banner_message());
		
		$banner['user_block'] = get_user_box();
		
		$this->template->set_template('page');
		$this->template->write_view('header','home/master/header',$header , false);
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		$this->template->write_view('search_form','home/master/page_search');
		$this->template->write_view('content', 'banner',$data);
		$this->template->write_view('footer','home/master/footer');
		$this->template->render();
	}
	
	function banner_send()
	{
		if($_SERVER['REQUEST_METHOD']!='POST'){
			redirect(site_url().'banner');
		}
		
		$this->load->library('cimailer','cimailer');
		$file = APPPATH.'../assets/mail/banner.txt';
		$handle = fopen($file,'r');
		$body = '';
		if ($handle) {
			while (!feof($handle)) {
				$file_ = fgets($handle, 4096);
				$body .= $file_."<br>";
			}
			fclose($handle);
		}
		
		try{
			//$this->cimailer->IsMail();
			$this->cimailer->CharSet="utf-8";
			$this->cimailer->IsHTML(true);
			
			$body = str_replace('{NAME}',$_POST['firstname'],$body);
			$body = str_replace('{LASTNAME}',$_POST['lastname'],$body);
			$body = str_replace('{EMAIL}',$_POST['email'],$body);
			$body = str_replace('{TEL}',$_POST['tel'],$body);
			$body = str_replace('{POSITION}',$_POST['location'],$body);
			$body = str_replace('{AMOUNT}',$_POST['month'],$body);
			$body = str_replace('{DETAIL}',$_POST['detail'],$body);
			
			/** mail to client **/
			$this->cimailer->From = $this->config->item('main_email');
			$this->cimailer->FromName = $this->config->item('site_name').' : '.$this->lang->line('banner_mail_title');
			$this->cimailer->AddReplyTo($this->config->item('main_email'), $this->config->item('site_name').' : '.$this->lang->line('banner_mail_title'));
			$this->cimailer->AddAddress($_POST['email']);
			$this->cimailer->Subject = $this->config->item('site_name').' : '.$this->lang->line('banner_mail_title');
			$this->cimailer->Body = $body;
			
			/** mail to client **/
			//var_dump($this->cimailer);
			if($this->cimailer->Send()){
				$data['message'] = $this->lang->line('banner_complete');
			
				/** mail to admin **/
				$this->cimailer->ClearAllRecipients();
				$this->cimailer->ClearReplyTos();
				$this->cimailer->From = $_POST['email'];
				$this->cimailer->FromName = $_POST['firstname'].' '.$_POST['lastname'];
				$this->cimailer->AddReplyTo($_POST['email'] , $_POST['firstname'].' '.$_POST['lastname']);
				$this->cimailer->AddAddress($this->config->item('main_email'),$this->config->item('site_name').' : '.$this->lang->line('banner_mail_title'));
				$this->cimailer->Subject = $this->config->item('site_name').' : '.$this->lang->line('banner_mail_title');
				$this->cimailer->Body = $body;
				$this->cimailer->Send();
				/** mail to admin **/
			}else{
				$data['message'] = $this->lang->line('mail_not_send');
				$data['message'] .= '<br />'.$this->cimailer->ErrorInfo;
			}
		} catch (phpmailerException $e) {
			echo $e->errorMessage(); //Pretty error messages from PHPMailer
			$data['message'] = $this->lang->line('mail_not_send');
		} catch (Exception $e) {
			echo $e->getMessage(); //Boring error messages from anything else!
			$data['message'] = $this->lang->line('mail_not_send');
		}
		
		$banner['user_block'] = get_user_box();
		$data['block_title'] = $this->lang->line('banner');
		$header['title'] = $this->config->item('site_name').' : '.$this->lang->line('banner');
		
		$this->template->set_template('page');
		$this->template->write_view('header','home/master/header',$header , false);
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		$this->template->write_view('search_form','home/master/page_search');
		$this->template->write_view('content', 'result_message',$data);
		$this->template->write_view('footer','home/master/footer');
		$this->template->render();
	}
	
	function subscribe()
	{
		$header['title'] = $this->config->item('site_name').' : '.$this->lang->line('subscribe');
		$this->template->set_template('page');
		$this->template->write_view('header','home/master/header',$header , false);
		$data['block_title'] = $this->lang->line('subscribe');
		//$banner = $this->banner_model->get_banner();
		$banner['user_block'] = get_user_box();
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		
		$this->template->write_view('search_form','home/master/page_search');
		$this->template->write_view('content', 'subscribe',$data);
		$this->template->write_view('footer','home/master/footer');
		$this->template->render();
	}
	
	function subscribe_register()
	{
		if(isset($_POST['name']) AND $_POST['name']!='' AND isset($_POST['email']) AND $_POST['email']!=''){
			$this->load->model('admin/subscribe_model','subscribe_model');
			$subscribe['name'] = $_POST['name'];
			$subscribe['email'] = $_POST['email'];
			$result = $this->subscribe_model->get($subscribe['email']);
			
			if(isset($result['email'])){
				$id = $result['id'];
			}else{
				$id = $this->subscribe_model->add($subscribe);
			}
			
			if($id){
				$this->load->library('cimailer','cimailer');
				$file = APPPATH.'../assets/mail/user_subscribe.txt';
				$handle = fopen($file,'r');
				$body = '';
				if ($handle) {
					while (!feof($handle)) {
						$file_ = fgets($handle, 4096);
						$body .= $file_."<br>";
					}
					fclose($handle);
				}
				
				try{
					//$this->cimailer->IsMail();
					$this->cimailer->CharSet="utf-8";
					$this->cimailer->IsHTML(true);
					$this->cimailer->From = $this->config->item('main_email');
					
					$body = str_replace('{NAME}',$_POST['name'],$body);
					$body = str_replace('{SITENAME}',$this->config->item('site_name'),$body);
					$body = str_replace('{CONFIRMLINK}',base_url().'subscribe/confirm/'.md5($id),$body);
					
					$this->cimailer->FromName = $this->config->item('site_name').' : '.$this->lang->line('subscribe');
					$this->cimailer->AddReplyTo($this->config->item('main_email'), $this->config->item('site_name').' : '.$this->lang->line('subscribe'));
					$this->cimailer->AddAddress($_POST['email']);
					$this->cimailer->Subject = $this->config->item('site_name').' : '.$this->lang->line('subscribe');
					$this->cimailer->Body = $body;
					$this->cimailer->Send();
					$data['message'] = $this->lang->line('subscribe_email');
				} catch (phpmailerException $e) {
					echo $e->errorMessage(); //Pretty error messages from PHPMailer
					$data['message'] = $this->lang->line('mail_not_send');
				} catch (Exception $e) {
					echo $e->getMessage(); //Boring error messages from anything else!
					$data['message'] = $this->lang->line('mail_not_send');
				}
			}else{
				$data['message'] = $this->lang->line('data_not_save');
			}
			
			$banner['user_block'] = get_user_box();
			$data['block_title'] = $this->lang->line('subscribe');
			$header['title'] = $this->config->item('site_name').' : '.$this->lang->line('subscribe');
			
			$this->template->set_template('page');
			$this->template->write_view('header','home/master/header',$header , false);
			$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
			$this->template->write_view('sideright','home/master/sidebar_right',$banner);
			$this->template->write_view('search_form','home/master/page_search');
			$this->template->write_view('content', 'result_message',$data);
			$this->template->write_view('footer','home/master/footer');
			$this->template->render();
		}else{
			redirect(site_url().'subscribe');
		}
	}
	
	function subscribe_email_existed()
	{
		$this->load->model('admin/subscribe_model','subscribe_model');
		$email = $_GET['email'];
		$result = $this->subscribe_model->get($email);
		
		if(isset($result['email'])){
			echo 'false';
		}else{
			echo 'true';
		}
	}
	
	function subscribe_confirm($sec_code='')
	{
		if($sec_code!=''){
			$this->load->model('admin/subscribe_model','subscribe_model');
			if($this->subscribe_model->confirm($sec_code)){
				$data['message'] = str_replace('{SITENAME}',$this->config->item('site_name'),$this->lang->line('subscribe_confirm'));
			}else{
				$data['message'] = $this->lang->line('data_not_save');
			}
		}else{
			$data['message'] = $this->lang->line('wrong_seccode');
		}
		
		$banner['user_block'] = get_user_box();
		$data['block_title'] = $this->lang->line('subscribe');
		$header['title'] = $this->config->item('site_name').' : '.$this->lang->line('subscribe');
		
		$this->template->set_template('page');
		$this->template->write_view('header','home/master/header',$header , false);
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		$this->template->write_view('search_form','home/master/page_search');
		$this->template->write_view('content', 'result_message',$data);
		$this->template->write_view('footer','home/master/footer');
		$this->template->render();
	}
	
	function un_subscribe($sec_code='')
	{
		if($sec_code!=''){
			$this->load->model('admin/subscribe_model','subscribe_model');
			if($this->subscribe_model->un_subscribe($sec_code)){
				$data['message'] = $this->lang->line('un_subscribe');
			}else{
				$data['message'] = $this->lang->line('data_not_save');
			}
		}else{
			$data['message'] = $this->lang->line('wrong_seccode');
		}
		
		$banner['user_block'] = get_user_box();
		$data['block_title'] = $this->lang->line('subscribe');
		$header['title'] = $this->config->item('site_name').' : '.$this->lang->line('subscribe');
		
		$this->template->set_template('page');
		$this->template->write_view('header','home/master/header',$header , false);
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		$this->template->write_view('search_form','home/master/page_search');
		$this->template->write_view('content', 'result_message',$data);
		$this->template->write_view('footer','home/master/footer');
		$this->template->render();
	}
	
	function paid_form()
	{
		$header['title'] = $this->config->item('site_name').' : '.$this->lang->line('paid_form');
		$data['block_title'] = $this->lang->line('paid_form');
		$data['email'] = '';
		$data['firstname'] = '';
		$data['lastname'] = '';
		$data['tel'] = '';
		
		if(($this->session->userdata('email'))!=''){
			$data['email'] = $this->session->userdata('email');
			$user_info = $this->user_model->getUserInfo($data['email']);
			$data['firstname'] = $user_info['firstname'];
			$data['lastname'] = $user_info['lastname'];
			$data['tel'] = $user_info['tel'];
		}
		$banner['user_block'] = get_user_box();
		
		$this->template->set_template('page');
		$this->template->write_view('header','home/master/header',$header , false);
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		$this->template->write_view('search_form','home/master/page_search');
		$this->template->write_view('content', 'paid_form',$data);
		$this->template->write_view('footer','home/master/footer');
		$this->template->render();
	}
	
	function paid_form_send()
	{
		$this->load->library('cimailer','cimailer');
		$file = APPPATH.'../assets/mail/paid_form.txt';
		$handle = fopen($file,'r');
		$body = '';
		if ($handle) {
			while (!feof($handle)) {
				$file_ = fgets($handle, 4096);
				$body .= $file_."<br>";
			}
			fclose($handle);
		}
		
		try{
			//$this->cimailer->IsMail();
			$this->cimailer->CharSet="utf-8";
			$this->cimailer->IsHTML(true);
			$this->cimailer->From = $this->config->item('main_email');
			
			$body = str_replace('{NAME}',$_POST['firstname'],$body);
			$body = str_replace('{LASTNAME}',$_POST['lastname'],$body);
			$body = str_replace('{EMAIL}',$_POST['email'],$body);
			$body = str_replace('{TEL}',$_POST['tel'],$body);
			$body = str_replace('{SUBJECT}',$_POST['subject'],$body);
			$body = str_replace('{DETAIL}',$_POST['detail'],$body);
			
			$this->cimailer->FromName = $this->config->item('site_name').' : '.$this->lang->line('paid_form');
			$this->cimailer->AddReplyTo($this->config->item('main_email'), $this->config->item('site_name').' : '.$this->lang->line('paid_form'));
			$this->cimailer->AddAddress($_POST['email']);
			$this->cimailer->Subject = $this->config->item('site_name').' : '.$this->lang->line('paid_form');
			$this->cimailer->Body = $body;
			$this->cimailer->Send();
			$data['message'] = $this->lang->line('paid_form_complete');
		} catch (phpmailerException $e) {
			echo $e->errorMessage(); //Pretty error messages from PHPMailer
			$data['message'] = $this->lang->line('mail_not_send');
		} catch (Exception $e) {
			echo $e->getMessage(); //Boring error messages from anything else!
			$data['message'] = $this->lang->line('mail_not_send');
		}
		
		$banner['user_block'] = get_user_box();
		$data['block_title'] = $this->lang->line('paid_form');
		$header['title'] = $this->config->item('site_name').' : '.$this->lang->line('paid_form');
		
		$this->template->set_template('page');
		$this->template->write_view('header','home/master/header',$header , false);
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		$this->template->write_view('search_form','home/master/page_search');
		$this->template->write_view('content', 'result_message',$data);
		$this->template->write_view('footer','home/master/footer');
		$this->template->render();
	}
	
	public function search()
	{
		$this->load->model('admin/company_model','company_model');
		$this->load->model('admin/country_model','country_model');
		$this->load->model('admin/province_model','province_model');
		
		$page_limit = $this->config->item('company_per_page');
		$page = 1;
		if(isset($_GET['page'])){
			$page = $_GET['page'];
		}
		
		$search['maincat'] = 0;
		$search['subcat'] = 0;
		$search['geography'] = 0;
		$search['province'] = 0;
		
		$search['product_type'] = 'Enter Keyword';
		$search['product_brand'] = 'Enter Keyword';
		$search['company_name'] = 'Enter Keyword';
		$search['search_text1'] = 'checked="checked"';
		$search['search_text2'] = '';
		
		if(!isset($_GET['maincat']) AND !isset($_GET['product_type'])){
			$count_company = count($this->company_model->get_company_lists(0,1,0,0,1,0));
			$count_gold_company = count($this->company_model->get_company_lists(0,1,0,2,1,0));
			$gold_page = ceil($count_gold_company/$page_limit);
			$data['company_nav'] = get_company_nav($count_company , $page_limit , $page , site_url().'search?page=%page%');
			
			$main_cat_gold = array();
			$main_cat_general = array();
			$data['company_list'] = array();
			
			$main_cat_gold = $this->company_model->get_company_lists(0,$page,$page_limit,2,1,0);
			$i = 0;
			foreach($main_cat_gold as $v){
				if($i%2){
					$data['company_list'][$i]['class'] = 'odd';
				}else{
					$data['company_list'][$i]['class'] = 'even';
				}
				$data['company_list'][$i]['id'] = $v->id;
				$data['company_list'][$i]['name'] = $v->name;
				$data['company_list'][$i]['address'] = $v->address;
				$data['company_list'][$i]['district'] = $v->district;
				$data['company_list'][$i]['province'] = $this->province_model->getProvinceNameByCatId($v->province);
				$data['company_list'][$i]['postcode'] = $v->postcode;
				$data['company_list'][$i]['country'] = $this->country_model->getCountryNameByCatId($v->country);
				$data['company_list'][$i]['logo'] = $v->logo;
				$data['company_list'][$i]['img'] = $v->img;
				$data['company_list'][$i]['website_name'] = str_replace('http://','',$v->website);
				$data['company_list'][$i]['website'] = 'http://'.str_replace('http://','',$v->website);
				$data['company_list'][$i]['email'] = $v->email;
				$data['company_list'][$i]['tel'] = $v->tel;
				$data['company_list'][$i]['fax'] = $v->fax;
				$data['company_list'][$i]['brand'] = $v->brand;
				$data['company_list'][$i]['email'] = $v->email;$data['company_list'][$i]['gold_icon'] = '<img src="'.base_url().'assets/image/gold.gif" alt="สมาชิก Gold Supplier" title="สมาชิก Gold Supplier" class="gold_icon" />';
				$data['company_list'][$i]['agent_icon'] = '';
				if($v->agent==1){
					$data['company_list'][$i]['agent_icon'] = '<img src="'.base_url().'assets/image/agent.gif" alt="เปิดรับตัวแทนจำหน่าย" title="เปิดรับตัวแทนจำหน่าย" class="agent_icon"  />';
				}
				$i++;
			}
			
			$g = $page_limit-$i;
			if($page>=$gold_page AND $g>0){
				if($page==$gold_page){
					$page = 1;
					$start_plus = 0;
					$page_limit = $g;
				}else{
					$start_plus = $page_limit-($count_gold_company%$page_limit);
					$g = 0;
					$page -= ($gold_page);
					$i = 0;
				}
				
				$main_cat_general = $this->company_model->get_company_lists(0,$page,$page_limit,1,0,0,$start_plus);
				foreach($main_cat_general as $v){
					if($i%2){
						$data['company_list'][$i]['class'] = 'odd';
					}else{
						$data['company_list'][$i]['class'] = 'even';
					}
					$data['company_list'][$i]['id'] = $v->id;
					$data['company_list'][$i]['name'] = $v->name;
					$data['company_list'][$i]['address'] = $v->address;
					$data['company_list'][$i]['district'] = $v->district;
					$data['company_list'][$i]['province'] = $this->province_model->getProvinceNameByCatId($v->province);
					$data['company_list'][$i]['postcode'] = $v->postcode;
					$data['company_list'][$i]['country'] = $this->country_model->getCountryNameByCatId($v->country);
					$data['company_list'][$i]['logo'] = $v->logo;
					$data['company_list'][$i]['img'] = $v->img;
					$data['company_list'][$i]['website_name'] = str_replace('http://','',$v->website);
					$data['company_list'][$i]['website'] = 'http://'.str_replace('http://','',$v->website);
					$data['company_list'][$i]['email'] = $v->email;
					$data['company_list'][$i]['tel'] = $v->tel;
					$data['company_list'][$i]['fax'] = $v->fax;
					$data['company_list'][$i]['brand'] = $v->brand;
					$data['company_list'][$i]['email'] = $v->email;
					$data['company_list'][$i]['gold_icon'] = '';
					$data['company_list'][$i]['agent_icon'] = '';
					if($v->agent==1){
						$data['company_list'][$i]['agent_icon'] = '<img src="'.base_url().'assets/image/agent.gif" alt="เปิดรับตัวแทนจำหน่าย" title="เปิดรับตัวแทนจำหน่าย" class="agent_icon"  />';
					}
					$i++;
				}
			}
		}else{
			$query_string = $_SERVER['QUERY_STRING'];
			$main_cat_gold = array();
			$main_cat_general = array();
			if(strpos($query_string,'page=')){
				$str = explode('&',$query_string);
				$num = count($str)-1;
				$i = 0;
				$query_string = '';
				for($i;$i<$num;$i++){
					$query_string .= $str[$i].'&';
				}
				
				$url = site_url().'search?'.$query_string.'page=%page%';
			}else{
				$url = site_url().'search?'.$query_string.'&page=%page%';
			}
			
			if(isset($_GET['maincat'])){
				$search['maincat'] = $_GET['maincat'];
				$search['subcat'] = $_GET['subcat'];
				
				if(isset($_GET['geography'])){
					$search['geography'] = $_GET['geography'];
				}else{
					$search['geography'] = 0;
				}
				
				$search['province'] = $_GET['province'];
				
				$main_cat_gold = $this->company_model->search1(0,$page,$page_limit,2,0,0,$_GET);
				
				$count_company = count($this->company_model->search1(0,1,0,0,1,0,$_GET));
				$count_gold_company = count($this->company_model->search1(0,1,0,2,1,0,$_GET));
				$gold_page = ceil($count_gold_company/$page_limit);
				$data['company_nav'] = get_company_nav($count_company , $page_limit , $page , $url);
			}else{
				$search['product_type'] = $_GET['product_type'];
				$search['product_brand'] = $_GET['product_brand'];
				$search['company_name'] = $_GET['company_name'];
				
				if($_GET['search_text']==1){
					$search['search_text1'] = 'checked="checked"';
					$search['search_text2'] = '';
				}else{
					$search['search_text1'] = '';
					$search['search_text2'] = 'checked="checked"';
				}
				
				$main_cat_gold = $this->company_model->search2(0,$page,$page_limit,2,0,0,$_GET);
				
				$count_company = count($this->company_model->search2(0,1,0,0,1,0,$_GET));
				$count_gold_company = count($this->company_model->search2(0,1,0,2,1,0,$_GET));
				$gold_page = ceil($count_gold_company/$page_limit);
				$data['company_nav'] = get_company_nav($count_company , $page_limit , $page , $url);
			}
			
			$data['company_list'] = array();
			$i = 0;
			//echo $this->db->last_query();
			foreach($main_cat_gold as $v){
				if($i%2){
					$data['company_list'][$i]['class'] = 'odd';
				}else{
					$data['company_list'][$i]['class'] = 'even';
				}
				$data['company_list'][$i]['id'] = $v->id;
				$data['company_list'][$i]['name'] = $v->name;
				$data['company_list'][$i]['address'] = $v->address;
				$data['company_list'][$i]['district'] = $v->district;
				$data['company_list'][$i]['province'] = $this->province_model->getProvinceNameByCatId($v->province);
				$data['company_list'][$i]['postcode'] = $v->postcode;
				$data['company_list'][$i]['country'] = $this->country_model->getCountryNameByCatId($v->country);
				$data['company_list'][$i]['logo'] = $v->logo;
				$data['company_list'][$i]['img'] = $v->img;
				$data['company_list'][$i]['website_name'] = str_replace('http://','',$v->website);
				$data['company_list'][$i]['website'] = 'http://'.str_replace('http://','',$v->website);
				$data['company_list'][$i]['email'] = $v->email;
				$data['company_list'][$i]['tel'] = $v->tel;
				$data['company_list'][$i]['fax'] = $v->fax;
				$data['company_list'][$i]['brand'] = $v->brand;
				$data['company_list'][$i]['email'] = $v->email;
				$data['company_list'][$i]['gold_icon'] = '<img src="'.base_url().'assets/image/gold.gif" alt="สมาชิก Gold Supplier" title="สมาชิก Gold Supplier" class="gold_icon" />';
				$data['company_list'][$i]['agent_icon'] = '';
				if($v->agent==1){
					$data['company_list'][$i]['agent_icon'] = '<img src="'.base_url().'assets/image/agent.gif" alt="เปิดรับตัวแทนจำหน่าย" title="เปิดรับตัวแทนจำหน่าย" class="agent_icon"  />';
				}
				$i++;
			}
			
			$g = $page_limit-$i;
			if($page>=$gold_page AND $g>0){
				//if($page==$gold_page){
				//	$page = 1;
				//	$start_plus = 0;
				//	$page_limit = $g;
				//}else{
				//	$start_plus = $page_limit-($count_gold_company%$page_limit);
				//	$g = 0;
				//	$page -= ($gold_page);
				//	$i = 0;
				//}
				
				if($gold_page==0){
					$start_plus = 0;
				}
				else if($page==$gold_page){
					$page = 1;
					$start_plus = 0;
					$page_limit = $g;
				}else{
					$start_plus = $page_limit-($count_gold_company%$page_limit);
					$g = 0;
					$page -= ($gold_page);
					$i = 0;
				}
				
				if(isset($_GET['maincat'])){
					$main_cat_general = $this->company_model->search1(0,$page,$page_limit,1,0,0,$_GET,$start_plus);
				}else{
					$main_cat_general = $this->company_model->search2(0,$page,$page_limit,1,0,0,$_GET,$start_plus);
				}
				
				foreach($main_cat_general as $v){
					if($i%2){
						$data['company_list'][$i]['class'] = 'odd';
					}else{
						$data['company_list'][$i]['class'] = 'even';
					}
					$data['company_list'][$i]['id'] = $v->id;
					$data['company_list'][$i]['name'] = $v->name;
					$data['company_list'][$i]['address'] = $v->address;
					$data['company_list'][$i]['district'] = $v->district;
					$data['company_list'][$i]['province'] = $this->province_model->getProvinceNameByCatId($v->province);
					$data['company_list'][$i]['postcode'] = $v->postcode;
					$data['company_list'][$i]['country'] = $this->country_model->getCountryNameByCatId($v->country);
					$data['company_list'][$i]['logo'] = $v->logo;
					$data['company_list'][$i]['img'] = $v->img;
					$data['company_list'][$i]['website_name'] = str_replace('http://','',$v->website);
					$data['company_list'][$i]['website'] = 'http://'.str_replace('http://','',$v->website);
					$data['company_list'][$i]['email'] = $v->email;
					$data['company_list'][$i]['tel'] = $v->tel;
					$data['company_list'][$i]['fax'] = $v->fax;
					$data['company_list'][$i]['brand'] = $v->brand;
					$data['company_list'][$i]['email'] = $v->email;
					$data['company_list'][$i]['gold_icon'] = '';
					$data['company_list'][$i]['agent_icon'] = '';
					if($v->agent==1){
						$data['company_list'][$i]['agent_icon'] = '<img src="'.base_url().'assets/image/agent.gif" alt="เปิดรับตัวแทนจำหน่าย" title="เปิดรับตัวแทนจำหน่าย" class="agent_icon"  />';
					}
					$i++;
				}
			}
		}
		
		$header['title'] = config_item('site_name').' : '.$this->lang->line('search_result');
		$data['block_title'] = $this->lang->line('search_result');
		$this->template->set_template('page');
		$this->template->write_view('header','master/header',$header , true);
		
		$banner['user_block'] = get_user_box();
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		
		$this->template->write_view('search_form','master/page_search_result',$search);
		$this->template->write_view('content', 'search',$data);
		$this->template->write_view('footer','master/footer');
		$this->template->render();
	}
	
	public function sitemap()
	{
		$this->load->model('admin/category_model','category_model');
		$main_cat = $this->category_model->lists(0,1,500);
		$data['catlist'] = array();
		$i = 0;
		foreach($main_cat as $v){
			$data['catlist'][$i]['id'] = $v->id;
			$data['catlist'][$i]['name_th'] = $v->name_th;
			$data['catlist'][$i]['sub'] = $this->category_model->lists($v->id,1,500);
			$i++;
		}
		
		$header['title'] = config_item('site_name').' : '.$this->lang->line('sitemap');
		$data['block_title'] = $this->lang->line('sitemap');
		
		$this->template->set_template('page');
		$this->template->write_view('header','master/header',$header , false);
		
		//$banner = $this->banner_model->get_banner();
		$banner['user_block'] = get_user_box();
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		
		$this->template->write_view('search_form','master/page_search');
		$this->template->write_view('content', 'sitemap' , $data);
		$this->template->write_view('footer','master/footer');
		$this->template->render();
	}
}

/* End of file home.php */
/* Location: ./application/modules/home/controllers/home.php */